--
-- 2024-8-26
--

--  while true do .... end
---------------------
---- fn<function>:
------ -1    ctn = 0, break
------ 0     执行等待继续while,
------ 1    ctn = 1, break
------ 2    goto
-----------------------
---- maxn      最多循环次数  <默认 100次>
---- sleepNum  mSleep(??)   <默认 延迟100毫秒>
function RxWhileDo(fn, maxn, x, y, sleepNum)
  maxn = maxn or 100
  sleepNum = sleepNum or 100
  if (not fn) then return 0 end

  local ctn = 0

  function _isFn()
    local __fn = nil
    if (type(fn) == 'function') then
      __fn = fn
    else
      __fn = function(x) return -1 end
    end
    return __fn
  end

  local _fn = _isFn()

  local n = 0
  while true do
    n = n + 1
    if n > maxn then break end
    ::__restart__::

    local fnCtn = _fn(n) or 0
    if (fnCtn == -1) then
      ctn = 0
      break
    elseif (fnCtn == 1) then
      ctn = 1
      break
    elseif (fnCtn == 2) then
      mSleep(sleepNum)
      UtilAutoPointColor(x, y)
      GameAlertRedPack()
      goto __restart__
    end

    mSleep(sleepNum)
    UtilAutoPointColor(x, y)
    GameAlertRedPack()
  end

  mSleep(sleepNum)
  UtilAutoPointColor(x, y)
  GameAlertRedPack()
  return ctn
end